/**
 * Header for functionality of Call Object module.
 */

#ifndef _CALL_OBJ_H_
#define _CALL_OBJ_H_

#include <stdint.h>

#include "../../core/str.h"

/**
 * Initialize call object module.
 *
 * /return 0 on success.
 */
int cobj_init(int c_start, int c_end);

/**
 * Close call object module.
 */
void cobj_destroy(void);

/**
 * Get a free object.
 *
 * /param timestamp assign this timestamp to the object we get.
 * /param callid pointer to callid str.
 * /return -1 if an error happens.
 * /return number of a free object on success.
 */
int cobj_get(uint64_t timestamp, str *callid);

/**
 * Free an Object
 *
 * /param num number of object to free
 * /return 0 on success
 */
int cobj_free(int num);

/**
 * Structure to store module statistics.
 */
typedef struct {
	int start;
	int end;
	int assigned;
} cobj_stats_t;

/**
 * Fill data in cobj_stats_t structure passed as pointer.
 *
 * /param stats pointer to cobj_stats_t structure.
 * /return 0 on success
 */
int cobj_stats_get(cobj_stats_t *stats);

/**
 * Free all objects at once.
 */
void cobj_free_all(void);

/**
 * Element of a returned object list.
 */
typedef struct _cobj_elem {
	int number;
	uint64_t timestamp;	
	str callid;
	/* TODO */
	struct _cobj_elem *next;
} cobj_elem_t;

/**
 * Free an object list.
 *
 * /param elem pointer to first element in the list.
 */
void cobj_free_list(cobj_elem_t *elem);

/**
 * Get current timestamp in milliseconds.
 *
 * /param ts pointer to timestamp integer.
 * /return 0 on success.
 */
int get_timestamp(uint64_t *ts);

/**
 * Get all objects which timestamp is less than or equals some value.
 *
 * User shall free returned list when not used any more.
 *
 * /param ts timestamp to compare.
 * /param elem returned list. NULL on error of if zero elements.
 * /param limit maximum number of objects to return. 0 means unlimited.
 *
 * /return number of returned objects on success.
 * /return -1 on error
 */
int cobj_get_timestamp(uint64_t ts, cobj_elem_t **elem, int limit);

#endif // _CALL_OBJ_H_
